<template>
  <div class="container">
    <div class="left">
        <div class="title">雪梨壁纸管理系统</div>
    </div>
    <div class="right">
        <div class="login-container">
            <div class="login-title">欢迎登录</div>
            <div class="login-desc">请输入您的账号和密码</div>
            <el-form  :model="form" label-width="auto" style="width: 300px">
                <div class="label">用户名</div>
                <el-form-item prop="username">
                    <el-input :prefix-icon="User" type="text" v-model="form.username" />
                </el-form-item>
                <div class="label">密码</div>
                <el-form-item prop="password">
                    <el-input :prefix-icon="Lock" type="password" v-model="form.password" show-password />
                </el-form-item>
            </el-form>
            <div class="button">
                <el-button :disabled="loading" :loading="loading" @click="login" type="primary" style="width: 100%;">登录</el-button>
            </div>
        </div>
    </div>
  </div>
</template>

<script setup>
import {ref,reactive,onBeforeMount} from 'vue'
import {useMainStore} from '@/utils/store'
import { Login } from '@/service/api/admin'
import {useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Lock,User } from '@element-plus/icons-vue'
const router=useRouter()
const store=useMainStore()
let loading=ref(false)
let form=reactive({
    username:'',
    password:''
})

onBeforeMount(()=>{
    gohome()
})

function gohome(){
    if(localStorage.getItem('accessToken')&&store.isfirst)router.push({name:'Home'})
    store.isfirst=false
}

function login(){
    loading.value=true
    Login({
        admin_username:form.username,
        admin_password:form.password
    }).then(res=>{
        loading.value=false
        if(res.data.code==200){
            localStorage.setItem('accessToken',res.data.accessToken)
            localStorage.setItem('refreshToken',res.data.refreshToken)
            ElMessage.success('登录成功')
            router.push({name:'Home'})
        }
    })
}
</script>


<style lang="scss" scoped>
.container{
    width: 100vw;
    height: 100vh;
    display: grid;
    grid-template-columns: 1fr 1fr;

    .left{
        height: 100%;
        background: url('../../public/bg.jpg');
        background-size: cover;
        display: flex;
        align-items: center;
        justify-content: center;

        .title{
            font-size:32px;
            color:#fff;
            font-weight: 500;
        }
    }

    .right{
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;

        .login-container{
            display: flex;
            flex-direction: column;
            align-items: center;

            .login-title{
                font-size: 22px;
                margin-bottom: 5px;
            }

            .login-desc{
                font-size: 12px;
                color: #5b5959;
                margin-bottom: 10px;
            }

            .label{
                font-size: 12px;
            }

            .button{
                width: 100%;
            }
        }
    }
}
</style>
